<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Evan
 * Date: 12-11-8
 * Time: 下午11:21
 * To change this template use File | Settings | File Templates.
 */
class SearchController  extends Jwawa_BaseController
{
    public function findSearchItemByPageCodeAction(){
        $param = $this->getJsonDataFromReq();
        $searchItem = Model_Cfg_SearchItem::findSearchItemByPageCode($param['page_code']);

        $rst = new Jwawa_ResponseBaseDto($searchItem);
        $json = Zend_Json::encode($rst);
        echo $json;
    }

    public function findSearchGrpByPageCodeAction(){
        $param = $this->getJsonDataFromReq();
        $searchGrp = Model_Cfg_SearchGroup::findSearchGrpByPageCode($param['page_code']);

        $rst = new Jwawa_ResponseBaseDto($searchGrp);
        $json = Zend_Json::encode($rst);
        echo $json;
    }

    public function findAllSearchGrpAndItemByPageCodeAction(){
        $param = $this->getJsonDataFromReq();
        $searchItemList = Model_Cfg_SearchItem::findSearchItemByPageCode($param['page_code']);
        $searchGroupList = Model_Cfg_SearchGroup::findSearchGrpByPageCode($param['page_code']);
        $tmp = array(
            'searchGroupList' => $searchGroupList,
            'searchItemList' => $searchItemList
        );
        $rst = new Jwawa_ResponseBaseDto($tmp);
        $json = Zend_Json::encode($rst);
        echo $json;
    }

    public function getAllSearchDataAction(){
        $param = $this->getJsonDataFromReq();
        $searchPageId = Model_Cfg_SearchPage::getPageIdByCode($param['page_code']);
        $searchGroupList = Model_Cfg_SearchGroup::findSearchGrpByPageId($searchPageId);

        $searchItemList = Model_Cfg_SearchItem::findSearchItemByPageId($searchPageId);
        $searchGrpItemShipList = Model_Cfg_SearchGroupItemShip::findSearchGrpAndItemShipByPageId($searchPageId);

        $tmp = array(
            'searchGroupList' => $searchGroupList,
            'searchItemList' => $searchItemList,
            'searchGrpItemShipList' => $searchGrpItemShipList
        );
        $rst = new Jwawa_ResponseBaseDto($tmp);
        $json = Zend_Json::encode($rst, 0);
        echo $json;
    }

    public function findAllSearchGroupWithDefaultByPageCodeAction(){
        $param = $this->getJsonDataFromReq();
        $searchGroupList = Model_Cfg_SearchGroup::findSearchGrpByPageCode($param['page_code']);
        $defaultSearchGroupId = null;
        $searchItemList = null;
        foreach($searchGroupList as $searchGroup){
            if($searchGroup->is_default == 'Y'){
                $defaultSearchGroupId = $searchGroup->id;
            }
        }

        if($defaultSearchGroupId){
            $searchItemList = Model_Cfg_SearchItem::findSearchItemByGrpId($defaultSearchGroupId);
        }

        $tmp = array(
            'defaultSearchGroupId' => $defaultSearchGroupId,
            'searchGroupList' => $searchGroupList,
            'searchItemList' => $searchItemList
        );
        $rst = new Jwawa_ResponseBaseDto($tmp);
        $json = Zend_Json::encode($rst, 0);
        echo $json;
    }

    public function findSearchItemByGroupIdAction(){
        $param = $this->getJsonDataFromReq();
        $searchItemList = Model_Cfg_SearchItem::findSearchItemByGrpId($param['group_id']);
        $tmp = array(
            'searchItemList' => $searchItemList
        );
        $rst = new Jwawa_ResponseBaseDto($tmp);
        $json = Zend_Json::encode($rst, 0);
        echo $json;
    }

    public function saveSearchGroupAction(){
        $param = $this->getJsonDataFromReq();
        $pageId = Model_Cfg_SearchPage::getPageIdByCode($param['page_code']);

        if(Model_Cfg_SearchGroup::isExistSearchGroupByName($pageId, $param['group_name'])){
            throw new Jwawa_BusinessException('ERROR_SEARCH_GROUP_EXISTED', array($param['group_name']));
        } else {
            $maxSeq = Model_Cfg_SearchGroup::getMaxSeqByPageId($pageId);
            $now = date("Y-m-d H:i:s");
            $tmp = array('id'=> $param['id'],
                         'page_id' => $pageId,
                         'group_name' => $param['group_name'],
                         'seq' => $maxSeq + 1,
                         'is_default' => 'N',
                         'create_by' => $this->getSessionBag()->getLoginName(),
                         'update_by' => $this->getSessionBag()->getLoginName(),
                         'create_time' => $now,
                         'update_time' => $now
            );
            $searchGroup = new Model_Cfg_SearchGroup($tmp);
            $db = Model_Cfg_SearchGroup::getDb();
            try{
                $db->beginTransaction();
                $searchGroup = Model_Cfg_SearchGroup::saveOrUpdate($searchGroup);

                $db->commit();
            } catch (Exception $e){
                $db->rollBack();
                throw $e;
            }
        }

        $rst = new Jwawa_ResponseBaseDto($searchGroup);
        $json = Zend_Json::encode($rst, 0);
        echo $json;
    }

    public function deleteSearchGroupAction(){
        $param = $this->getJsonDataFromReq();
        $db = Model_Cfg_SearchGroup::getDb();
        try{
            $db->beginTransaction();

            Model_Cfg_SearchGroup::deleteByIds($param);
            Model_Cfg_SearchGroupItemShip::deletBySearchGroupId($param);

            $db->commit();
        } catch (Exception $e){
            $db->rollBack();
            throw $e;
        }
        $rst = new Jwawa_ResponseBaseDto();
        $json = Zend_Json::encode($rst, 0);
        echo $json;
    }

    public function saveSearchGroupItemShipAction(){
        $param = $this->getJsonDataFromReq();
        $searchGroupId = $param['searchGroupId'];
        $searchItemList = $param['sarchGroupItemShipList'];

        $db = Model_Cfg_SearchGroup::getDb();
        try{
            $db->beginTransaction();
            Model_Cfg_SearchGroupItemShip::deletBySearchGroupId(array($searchGroupId));
            for($i = 0; $i < count($searchItemList); $i++){
                $tmp = array(
                    'search_group_id' => $searchGroupId,
                    'search_item_id' => $searchItemList[$i]['search_item_id'],
                    'seq' => $searchItemList[$i]['seq'],
                    'default_value' => $searchItemList[$i]['default_value']
                );
                $searchGroupItemShip = new Model_Cfg_SearchGroupItemShip($tmp);
                Model_Cfg_SearchGroupItemShip::save($searchGroupItemShip);
            }
            $db->commit();
        } catch (Exception $e){
            $db->rollBack();
            throw $e;
        }

        $rst = new Jwawa_ResponseBaseDto();
        $json = Zend_Json::encode($rst, 0);
        echo $json;
    }

    public function setDefaultSearchGroupAction(){
        $param = $this->getJsonDataFromReq();
        $groupId = intval($param['group_id']);
        $searchGroup = Model_Cfg_SearchGroup::getSearchGroupById($groupId);
        $now = date("Y-m-d H:i:s");

        $db = Model_Cfg_SearchGroup::getDb();
        try{
            $db->beginTransaction();
            Model_Cfg_SearchGroup::updateSearchGroupToNoneDefaultByPageId(intval($searchGroup->page_id));
            $tmp = array(
                'id' => $groupId,
                'update_by' => $this->getSessionBag()->getLoginName(),
                'update_time' => $now
            );
            $searchGroup = new  Model_Cfg_SearchGroup($tmp);
            Model_Cfg_SearchGroup::setSearchGroupToDefault($searchGroup);

            $db->commit();
        } catch (Exception $e){
            $db->rollBack();
            throw $e;
        }

        $rst = new Jwawa_ResponseBaseDto();
        $json = Zend_Json::encode($rst, 0);
        echo $json;
    }
}